;$(function () {
  $.ajax({
    url: "./cart/query",
    type: "get",
    data: {uid: JSON.parse(localStorage.getItem("userinfo"))[0].u_id}
  }).then(function (res) {
    layer.msg(res.msg)

    render(res.data);
    $(window).on("load", function () {
      $(".goodslist").allp()
    });

  })

  function render(list) {
    let strHtml = ``;
    list.forEach(el => {
      strHtml += `
        <li class="godsd" data-cid=${el.c_id}>
                    <div class="product_selected_shopName"><span class="le">乐融自营</span></div>
                    <div class="selected">
                        <input name="checkbox" type="checkbox" class="onec" id="ck">
                    </div>
                    <div class="contimg">
                        <img src=${el.c_img} alt="">
                        <div class="phonename">${el.c_name}</div>
                    </div>

                    <div class="oneprice"><span>${el.c_price}</span></div>
                    <div class="goodsc">
                        <div class="goods_num">
                            <div class="num_minus jian"><span >-</span></div>
                            <div class="num_text "><input type="text" value=${el.c_num} class="lzj"></div>
                            <div class="num_plus jia"><span >+</span></div>
                        </div>
                    </div>
                    <div class="inallprice pay">${el.c_total}</div>
                    <div class="remove">删除</div>
                </li>        
        `;

      $(".gdc").append(strHtml);

    })
  }

  $(".gdc").on("click", ".jian", function () {
    let num = $(this).next().find(".lzj").val();
    num--;
    if (num <= 1) {
      num = 1;
      layer.msg("至少购买一台")
    }
    $(this).next().find(".lzj").val(num);

    let total = $(this).parents("li").find(".oneprice").find("span").text() * num;
    $(this).parents("li").find(".pay").text(`${total} 元`);
    console.log($(this).parents("li"));
    $.ajax({
      url: "/cart/modify",
      type: "post",
      data: {
        // jq的 data()方法,获取自定义属性的值
        cid: $(this).parents("li").data("cid"),
        cnum: num
      }
    })
  })


  $(".gdc").on("click", ".jia", function () {
    let num = $(this).prev().find(".lzj").val();
    num++;
    if (num >= 10) {
      num = 10;
      layer.msg("最多购买10台")
    }
    $(this).prev().find(".lzj").val(num);
    let total = $(this).parents("li").find(".oneprice").find("span").text() * num;
    $(this).parents("li").find(".pay").text(`${total} 元`);

    $.ajax({
      url: "/cart/modify",
      type: "post",
      data: {
        // jq的 data()方法,获取自定义属性的值
        cid: $(this).parents("li").data("cid"),
        cnum: num
      }
    })
  })

  $(".gdc").on("click", ".remove", function () {
    let cId = $(this).parents("li").data("cid");
    let self = this;
    layer.confirm("您确定要这么做吗?", {
      btn: ["yes", "No"]
    }, function (index) {
      $.ajax({
        url: "/cart/delete",
        type: "post",
        data: {cId}
      }).done(function (res) {
        layer.msg(res.msg);

        if (res.status == 200) {

          $(self).parents("li").remove()
        }

      })
      layer.close(index);
      if ($(".gdc").find("li").length==1){
        $(".all").prop("checked",false)
      }
    }, function () {

    })

  })

})